package com.expense.util;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.expense.entity.UserInfo;

public class PaginatedList extends ArrayList {
	
	private int pages = 0; //总共页数
    private int currentPage = 0; //当前页
    private int pageSize = 0; //当前页面的大小
    private int totleCount = 0;//总共的记录
    private int nextIndex=0;//下一条记录
    private int startIndex;
    private int endIndex;

    private List dataList=new ArrayList();

    public PaginatedList() {
        super();
    }

 

    /**
     * <p> 设置页数
     * @param pages type of int
     * @return none
     * */
    public void setPages(int pages) {
        this.pages = pages;
    }

    /**
     * <p> 返回页数
     * @return pages type of int
     * */
    public int getPages() {
        return this.pages;
    }

    /**
     * <p> 设置当前页
     * @param currentPage type of int
     * @return none
     * */
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    /**
     * <p> 返回当前页
     * @return currentPage type of int
     * */
    public int getCurrentPage() {
        return this.currentPage;
    }

    /**
     * <p> 设置每页的记录
     * @param pageSize type of int
     * @return none
     * */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * <p> 返回每页的记录
     * @return pageSize type of int
     * */
    public int getPageSize() {
        return this.pageSize;
    }

    /**
     * <p> 设置总共多少条记录
     * @param size type of int
     * @return none
     * */
    public void setTotleCount(int size) {
        this.totleCount = size;
    }

    /**
     * <p> 返回总共多少条记录
     * @return size type of int
     * */
    public int getTotleCount() {
        return this.totleCount;
    }
    public int getNextIndex() {
        return nextIndex;
    }
    public void setNextIndex(int nextIndex) {
        this.nextIndex = nextIndex;
    }

	public int getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(int startIndex) {
		this.startIndex = startIndex;
	}



	public int getEndIndex() {
		return endIndex;
	}



	public void setEndIndex(int endIndex) {
		this.endIndex = endIndex;
	}



	public List getDataList() {
		return dataList;
	}



	public void setDataList(List dataList) {
		this.dataList = dataList;
	}
    
    
	public static int getCurrentPage(HttpServletRequest request) {
		String page = StringUtils.chgNull(request.getParameter(Constants.PAGE));
		page = (page.length() == 0) ? "1" : page;
		return StringUtils.parseInt(page, 1);
	}

	public static int getPageSize(HttpServletRequest request) {
		String pageSize = StringUtils.chgNull(request
				.getParameter(Constants.PAGE_SIZE));
		if (pageSize.equals("") || pageSize.equals("0")) {
			PropertiesReader reader = PropertiesReader.getIntance("/conf.properties");
			//return reader.getInt(Constants.PAGE_SIZE).intValue();
			pageSize=reader.getInt(Constants.PAGE_SIZE).toString();
		} 
		int pageSizeVal=StringUtils.parseInt(pageSize);
		if(pageSizeVal==0){
			pageSizeVal=10;
		}
		
		return pageSizeVal;

	}
	/*
	 * 获取DataGrid需要的Json数据
	 * */
	public String getGridData()
	{
		return "{\"total\":"+this.pages+",\"rows\":"+toJson()+"}";
	}
	/*
	 * 转换为Json格式数据
	 * */
	public String toJson()
	{
		StringBuilder sbJosn=new StringBuilder();
		sbJosn.append("[");
	    if(this.dataList!=null)
	    {
	    	for(Object model:dataList)
	    	{
	    		StringBuilder modelJson=new StringBuilder("{");
	        	Field[] fields=model.getClass().getDeclaredFields();
	    	    for(int i =0;i<fields.length;i++){
	        	       Field field = fields[i];
	        	       String name= field.getName();
	        	       Object value = null;
	        	       try {
	        	    	   field.setAccessible(true);
	        	    	   value=field.get(model);
						} catch (IllegalArgumentException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						// 如果是boolean或者int类型，不需要加双引号
						if (value instanceof Boolean ||value instanceof Integer) {
							value=value;
						}
						else
						{							
							value="\""+value+"\"";
						}
						modelJson.append("\""+name+ "\":"+value+",");
	    	    }
	
	        	// 如果属性，删除最后一个逗号
	            if(modelJson.length()>1)
	            	modelJson.deleteCharAt(modelJson.length()-1);
	            // 最后添加反大括号
	            modelJson.append("},");
	            // 添加到数组
	            sbJosn.append(modelJson);
	    	}
	    }
	    // 如果有值，删除最后一个逗号
	    if(sbJosn.length()>1)
	    	sbJosn.deleteCharAt(sbJosn.length()-1);
	    // 最后添加反中括号
	    sbJosn.append("]");
	    return sbJosn.toString();
	}

}

